package com.yoyo.web.controller.system;

import com.yoyo.common.config.Global;
import com.yoyo.common.core.domain.entity.SysMenu;
import com.yoyo.common.core.domain.entity.SysUser;
import com.yoyo.common.core.text.Convert;
import com.yoyo.common.utils.DateUtils;
import com.yoyo.common.utils.ServletUtils;
import com.yoyo.common.utils.ShiroUtils;
import com.yoyo.common.utils.StringUtils;
import com.yoyo.system.service.ISysConfigService;
import com.yoyo.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.Cookie;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @author zyl
 * @Date 2021/8/8 -9:49
 */
@Controller
public class SysIndexController {
    @Autowired
    ISysMenuService sysMenuService;
    @Autowired
    private ISysConfigService configService;

    @GetMapping("/index")
    public String index(ModelMap map) {
        SysUser sysUser = ShiroUtils.getSysUser();
        List<SysMenu> menus = sysMenuService.selectMenusByUser(sysUser);
        map.put("menus", menus);
        map.put("user", sysUser);
        map.put("sideTheme", configService.selectConfigByKey("sys.index.sideTheme"));
        map.put("skinName", configService.selectConfigByKey("sys.index.skinName"));
        map.put("ignoreFooter", configService.selectConfigByKey("sys.index.ignoreFooter"));
        map.put("copyrightYear", Global.getCopyrightYear());
        map.put("demoEnable", Global.isDemoEnabled());
        map.put("isDefaultModifyPwd", initPasswordIsModify(sysUser.getPwdUpdateDate()));
        map.put("isPasswordExpired", passwordIsExpiration(sysUser.getPwdUpdateDate()));

        // 菜单导航显示风格
        String menuStyle = configService.selectConfigByKey("sys.index.menuStyle");
        // 移动端，默认使左侧导航菜单，否则取默认配置
        String indexStyle = ServletUtils.checkAgentIsMobile(ServletUtils.getRequest().getHeader("User-Agent")) ? "index" : menuStyle;

        // 优先Cookie配置导航菜单
        Cookie[] cookies = ServletUtils.getRequest().getCookies();
        for (Cookie cookie : cookies) {
            if (StringUtils.isNotEmpty(cookie.getName()) && "nav-style".equalsIgnoreCase(cookie.getName())) {
                indexStyle = cookie.getValue();
                break;
            }
        }
        return "topnav".equalsIgnoreCase(indexStyle) ? "index-topnav" : "index";
    }

    private boolean passwordIsExpiration(LocalDateTime pwdUpdateDate) {
        int passwordValidateDays = Convert.toInt(configService.selectConfigByKey("sys.account.passwordValidateDays"));
        if (passwordValidateDays > 0) {
            if (StringUtils.isNull(pwdUpdateDate)) {
                // 如果从未修改过初始密码，直接提醒过期
                return true;
            }
            Date nowDate = DateUtils.getNowDate();
            return DateUtils.differentDaysByMillisecond(nowDate, DateUtils.LocalDateToDate(pwdUpdateDate)) > passwordValidateDays;
        }
        return false;
    }


    public boolean initPasswordIsModify(LocalDateTime pwdUpdateDate) {
        int initPasswordModify = Convert.toInt(configService.selectConfigByKey("sys.account.initPasswordModify"));
        return initPasswordModify == 1 && pwdUpdateDate == null;
    }
}
